package io.renren.modules.eightapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.modules.eightapp.dao.EiParkInfoDao;
import io.renren.modules.eightapp.dao.EiParkingInfoDao;
import io.renren.modules.eightapp.dto.EiParkInfoDTO;
import io.renren.modules.eightapp.dto.ExcelDTO;
import io.renren.modules.eightapp.entity.EiParkInfoEntity;
import io.renren.modules.eightapp.entity.EiParkingInfoEntity;
import io.renren.modules.eightapp.service.EiParkInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2022-12-28
 */
@Service
public class EiParkInfoServiceImpl extends CrudServiceImpl<EiParkInfoDao, EiParkInfoEntity, EiParkInfoDTO> implements EiParkInfoService {
    @Autowired
    private EiParkInfoDao eiParkInfoDao;
    @Autowired
    private EiParkingInfoDao eiParkingInfoDao;

    @Override
    public QueryWrapper<EiParkInfoEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<EiParkInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public void savePark(EiParkInfoDTO dto) {
        dto.setCreator("Test");
        dto.setCreateTime(new Date(11111111111111L));
        dto .setUpdateTime(new Date());
        dto.setParkuseCount(0);

        EiParkInfoEntity eiParkInfoEntity=new EiParkInfoEntity();
        BeanUtils.copyProperties(dto,eiParkInfoEntity);
        eiParkInfoDao.insert(eiParkInfoEntity);

        Integer sum = this.insertParkingInfoList(eiParkInfoEntity.getIcon(), eiParkInfoEntity,1);

        eiParkInfoEntity.setParkCount(sum);
        eiParkInfoDao.updateById(eiParkInfoEntity);
    }

    @Override
    public void updatePark(EiParkInfoDTO dto) {
        //查询停车场信息 并获取到原结构文件并删除
        EiParkInfoEntity eiParkInfoEntity = eiParkInfoDao.selectById(dto.getId());
        if (!eiParkInfoEntity.getIcon().equals(dto.getIcon())) {
            String filepath = System.getProperty("user.dir")+File.separator+"files"+File.separator+"park" + File.separator+eiParkInfoEntity.getIcon();
            File file = new File(filepath);
            if (file.exists()) {
                file.delete();
            }
            //读取新的结构文件 并将其序列化一个新的ID列表

            List<List> lists = ExcelUtils.readExcel(dto.getIcon());
            List<Integer> listAfter=new ArrayList<>();
            for (List<Integer> l:lists) {
                for (int i = 0; i <l.size() ; i++) {
                    if (l.get(i) >0) {
                        listAfter.add(l.get(i));
                    }
                }
            }
            //获取原来车位信息 序列成旧的ID列表
            QueryWrapper<EiParkingInfoEntity> queryWrapper=new QueryWrapper();
            queryWrapper.lambda().eq(EiParkingInfoEntity::getParkId,eiParkInfoEntity.getId());
            List<EiParkingInfoEntity> eiParkingInfoEntities = eiParkingInfoDao.selectList(queryWrapper);

            List<Integer> listBefore=new ArrayList<>();
            for (EiParkingInfoEntity e:eiParkingInfoEntities) {
                listBefore.add(e.getParkingId());
            }
            //获取到新来车位信息ID列表大小
            int afterNum=this.insertParkingInfoList(dto.getIcon(),null,0);

            eiParkInfoEntity.setIcon(dto.getIcon());
            eiParkInfoEntity.setParkCount(afterNum);

            //比较 两次车位信息ID列表大小
            if (afterNum>listBefore.size()) {
                //如果true 则为添加信息 插入
                listAfter.removeAll(listBefore);
                for (Integer i:listAfter) {
                    EiParkingInfoEntity eiParkingInfoEntity=new EiParkingInfoEntity();
                    eiParkingInfoEntity.setParkId(eiParkInfoEntity.getId());
                    eiParkingInfoEntity.setParkingId(i);
                    eiParkingInfoEntity.setState(0);
                    eiParkingInfoEntity.setUpdator(eiParkInfoEntity.getUpdator());
                    eiParkingInfoEntity.setCreator("Test");
                    eiParkingInfoEntity.setCreateTime(new Date(11111111111111L));
                    eiParkingInfoEntity .setUpdateTime(new Date());
                    eiParkingInfoDao.insert(eiParkingInfoEntity);
                }
            }else if(afterNum<listBefore.size()){
                listBefore.removeAll(listAfter);
                //如果为否则删除
                for (EiParkingInfoEntity e:eiParkingInfoEntities) {
                    for (Integer  i:listBefore) {
                        if (e.getParkingId()==i&&e.getState()==1) {
                            //如果是已使用状态 停车场使用数量-1
                            eiParkInfoEntity.setParkuseCount(eiParkInfoEntity.getParkuseCount()-1);
                            //并且删除使用人信息
                        }
                    }
                }

                UpdateWrapper<EiParkingInfoEntity> updateWrapper=new UpdateWrapper<>();
                updateWrapper.lambda().in(EiParkingInfoEntity::getParkingId,listBefore);
                eiParkingInfoDao.delete(updateWrapper);
            }
        }
        eiParkInfoEntity.setVillageId(dto.getVillageId());
        eiParkInfoEntity.setUpdator(dto.getUpdator());
        eiParkInfoDao.updateById(eiParkInfoEntity);
    }

    @Override
    public void deletePark(Long[] ids) {

        //删除停车场信息和结构文件
        QueryWrapper<EiParkInfoEntity> queryWrapper=new QueryWrapper();
        queryWrapper.lambda().in(EiParkInfoEntity::getId,ids);
        List<EiParkInfoEntity> eiParkInfoEntities = eiParkInfoDao.selectList(queryWrapper);
        for (EiParkInfoEntity e:eiParkInfoEntities) {
            String filepath = System.getProperty("user.dir")+File.separator+"files"+File.separator+"park" + File.separator+e.getIcon();
            File file = new File(filepath);
            if (file.exists()) {
                file.delete();
            }
        }

        UpdateWrapper<EiParkInfoEntity> wrapper=new UpdateWrapper<>();
        wrapper.lambda().in(EiParkInfoEntity::getId,ids);
        eiParkInfoDao.delete(wrapper);

        //删除车位信息和结构文件
        UpdateWrapper<EiParkingInfoEntity> updateWrapper=new UpdateWrapper<>();
        updateWrapper.lambda().in(EiParkingInfoEntity::getParkId,ids);
        eiParkingInfoDao.delete(updateWrapper);

    }

    public Integer insertParkingInfoList(String icon,EiParkInfoEntity eiParkInfoEntity,Integer type){
        int sum=0;
        List<List> lists = ExcelUtils.readExcel(icon);
        for (List<Integer> l:lists) {
            for (int i = 0; i < l.size(); i++) {
                if (l.get(i)>0) {
                    sum++;
                    if (type==0){
                        continue;
                    }
                    EiParkingInfoEntity eiParkingInfoEntity=new EiParkingInfoEntity();
                    eiParkingInfoEntity.setParkId(eiParkInfoEntity.getId());
                    eiParkingInfoEntity.setParkingId(Integer.parseInt(String.valueOf(l.get(i))));
                    eiParkingInfoEntity.setState(0);
                    eiParkingInfoEntity.setUpdator(eiParkInfoEntity.getUpdator());
                    eiParkingInfoEntity.setCreator("Test");
                    eiParkingInfoEntity.setCreateTime(new Date(11111111111111L));
                    eiParkingInfoEntity .setUpdateTime(new Date());
                    eiParkingInfoDao.insert(eiParkingInfoEntity);
                }
            }
        }

        return sum;
    }

}